package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.OrderCommentService;
import cn.icare.cms.StringUtil;
import cn.icare.cms.dao.mapper.OrderCommentMapper;
import cn.icare.cms.dao.po.Coupon;
import cn.icare.cms.dao.po.OrderComment;
import cn.icare.cms.model.ro.AddOrderCommentRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Classname OrderCommentServiceImpl
 * @Description TODO
 * @Date 2020/4/1 19:44
 * @Created by Administrator
 */
@Service
public class OrderCommentServiceImpl implements OrderCommentService {

    @Resource
    private OrderCommentMapper commentMapper;

    private final Logger logger = LoggerFactory.getLogger(OrderCommentServiceImpl.class);


    @Override
    public Integer createOne(AddOrderCommentRO ro, Long memberId, String memberName) {
        OrderComment orderComment = new OrderComment();
        orderComment.setMemberid(memberId);
        orderComment.setName(memberName);
        orderComment.setOrderid(ro.getOrderId());
        orderComment.setScoreFw(ro.getScore_fw());
        orderComment.setScoreMs(ro.getScore_ms());
        orderComment.setScoreWl(ro.getScore_wl());
        orderComment.setCreatetime(new Date());
        orderComment.setMemo(ro.getMemo());
        int result = this.commentMapper.insertSelective(orderComment);
        if (result > 0) {
            return orderComment.getId();
        }
        return 0;
    }

    @Override
    public boolean deleteOne(Integer id) {

        int result = this.commentMapper.deleteByPrimaryKey(id);
        return result > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(Coupon.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.commentMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public MsgDatasVO getList(Long orderId, String name, Integer pageNum, Integer pageSize) {

        MsgDatasVO result = new MsgDatasVO();
        try {
            Example example = new Example(OrderComment.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty(name)) {
                criteria.andLike("name", "%" + name + "%");
            }
            if (orderId != null && orderId > 0) {
                criteria.andEqualTo("orderid", orderId);
            }
            example.setOrderByClause("id desc");
            PageHelper.startPage(pageNum, pageSize);
            List<OrderComment> dataList = this.commentMapper.selectByExample(example);
            PageInfo pageInfo = new PageInfo(dataList);
            result.setTotal(pageInfo.getTotal());
            result.setDatas(dataList);
        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }
}
